package com.kerbores.blog.dao.impl;

import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.ContinueLoop;
import org.nutz.lang.Each;
import org.nutz.lang.ExitLoop;
import org.nutz.lang.Lang;
import org.nutz.lang.LoopException;

import com.kerbores.blog.bean.Comment;
import com.kerbores.blog.dao.ICommentDao;
import com.kerbores.utils.db.dao.impl.BaseDao;

/**
 * @author Ixion <br>
 *         create at 2014年11月1日
 */
@IocBean(name = "commentDao")
public class CommentDaoImpl extends BaseDao<Comment> implements ICommentDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.kerbores.utils.db.dao.impl.BaseDao#initClass()
	 */
	@Override
	public Class<Comment> initClass() {
		return Comment.class;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.kerbores.blog.dao.ICommentDao#findByArticleId(int)
	 */
	@Override
	public List<Comment> findByArticleId(int id) {
		List<Comment> comments = dao.query(initClass(), Cnd.where("articleId", "=", id).desc("id"));
		Lang.each(comments, new Each<Comment>() {
			@Override
			public void invoke(int index, Comment comment, int length) throws ExitLoop, ContinueLoop, LoopException {
				comment = dao.fetchLinks(comment, "commenter");
			}
		});
		return comments;
	}

}
